package com.xbongbong.parent.field.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.StageProcessEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.model.StageProcessModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.parent.field.FieldAnalyseService;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.saas.constant.BasicConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Description 触发条件阶段推进器字段
 * @Author sunshaoxiang
 * @Date 2021/12/7 16:36
 * @Version 1.0
 */
@Service("StageFieldAnalyseServiceImpl")
public class StageFieldAnalyseServiceImpl implements FieldAnalyseService {

    @Resource
    private ProFormHelp proFormHelp;
    @Resource
    private StageProcessModel stageProcessModel;

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.STAGE_THRUSTER.getType());
    }

    @Override
    public Object analysisData(Long formId, Integer businessType, Integer saasMark, PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity) throws XbbException {
        List<ItemPoJo> itemPoJos = proFormHelp.getEditStageComboItems(fieldAttrEntity, saasMark, businessType, paasFormDataEntity.getCorpid(), paasFormDataEntity.getFormId(), data);
        fieldAttrEntity.setItems(itemPoJos);
        return  ItemUtil.getCustomItemList(fieldAttrEntity, data);
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        Object object = filterCondition.getValue();
        String corpid = businessRuleInfoPojo.getCorpid();
        Integer businessType = businessRuleInfoPojo.getBusinessType();
        Integer saasMark = businessRuleInfoPojo.getSaasMark();
        Long formId = businessRuleInfoPojo.getFormId();
        if ( Objects.equals(ConditionEnum.IN.getSymbol(), filterCondition.getSymbol())
                || Objects.equals(ConditionEnum.NOIN.getSymbol(), filterCondition.getSymbol())) {
            if (object instanceof List) {
                List<Object> list = JSON.parseArray(JSON.toJSONString(object), Object.class);
                List<Object> resultList = new ArrayList<>();
                if (Objects.equals(thisField.getStageMultiVersion(), 1)) {
                    // 多版本启用
                    List<StageProcessEntity> stageProcessEntityList = stageProcessModel.getEnableStageProcessList(corpid, formId);
                    for (StageProcessEntity stageProcess : stageProcessEntityList) {
                        List<ItemPoJo> itemPoJos = proFormHelp.getItemPojoForStageEntity(corpid, formId, stageProcess.getId(), businessType, saasMark, BasicConstant.ONE);
                        thisField.setItems(itemPoJos);
                        for (Object item : list) {
                            try {
                                resultList.add(ItemUtil.getCustomItemValue(thisField, String.valueOf(item)));
                            } catch (XbbException e) {
                            }
                        }
                    }
                } else {
                    List<ItemPoJo> itemPoJos = proFormHelp.getEnableStageComboItems(saasMark, businessType, corpid, formId);
                    thisField.setItems(itemPoJos);
                    for (Object item : list) {
                        try {
                            resultList.add(ItemUtil.getCustomItemValue(thisField, String.valueOf(item)));
                        } catch (XbbException e) {
                        }
                    }
                }
                return resultList;
            }
        } else if (Objects.equals(ConditionEnum.LIKE.getSymbol(), filterCondition.getSymbol())
                || Objects.equals(ConditionEnum.NOLIKE.getSymbol(), filterCondition.getSymbol())) {
            if (Objects.equals(thisField.getStageMultiVersion(), 1)) {
                List<StageProcessEntity> stageProcessEntityList = stageProcessModel.getEnableStageProcessList(corpid, formId);
                List<ItemPoJo> itemPoJoList = new ArrayList<>();
                for (StageProcessEntity stageProcess : stageProcessEntityList) {
                    List<ItemPoJo> itemPoJos = proFormHelp.getItemPojoForStageEntity(corpid, formId, stageProcess.getId(), businessType, saasMark, BasicConstant.ONE);
                    itemPoJoList.addAll(itemPoJos);
                }
                thisField.setItems(itemPoJoList);
            } else {
                List<ItemPoJo> itemPoJos = proFormHelp.getEnableStageComboItems(saasMark, businessType, corpid, formId);
                thisField.setItems(itemPoJos);
            }
            return object;
        } else {
            if (object instanceof String) {
                if (Objects.equals(thisField.getStageMultiVersion(), 1)) {
                    List<Object> resultList = new ArrayList<>();
                    List<StageProcessEntity> stageProcessEntityList = stageProcessModel.getEnableStageProcessList(corpid, formId);
                    for (StageProcessEntity stageProcess : stageProcessEntityList) {
                        List<ItemPoJo> itemPoJos = proFormHelp.getItemPojoForStageEntity(corpid, formId, stageProcess.getId(), businessType, saasMark, BasicConstant.ONE);
                        thisField.setItems(itemPoJos);
                        try {
                            resultList.add(ItemUtil.getCustomItemValue(thisField, String.valueOf(object)));
                        } catch (XbbException e) {
                        }
                    }
                    object = resultList;
                    if (Objects.equals(ConditionEnum.EQUAL.getSymbol(), filterCondition.getSymbol())) {
                        filterCondition.setSymbol(ConditionEnum.IN.getSymbol());
                    } else if (Objects.equals(ConditionEnum.NOEQUAL.getSymbol(), filterCondition.getSymbol())) {
                        filterCondition.setSymbol(ConditionEnum.NOIN.getSymbol());
                    }
                } else {
                    List<ItemPoJo> itemPoJos = proFormHelp.getEnableStageComboItems(saasMark, businessType, corpid, formId);
                    thisField.setItems(itemPoJos);
                    object = ItemUtil.getCustomItemValue(thisField, String.valueOf(object));
                }
            }
        }
        return object;
    }
}
